package servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Database.DBConnection;

/**
 * Servlet implementation class StandardOpProcedure
 */
public class StandardOpProcedure extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public StandardOpProcedure() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processManualUrls(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processManualUrls(request, response);
	}

	private void processManualUrls(HttpServletRequest request, HttpServletResponse response) {
		try {
			 Connection connection1st = null;
			 Statement statement1st = null;
			 
			 connection1st = DBConnection.connect(true);
			 statement1st = connection1st.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

			 ArrayList<String> manualList = new ArrayList<String>();
			// close all the connections.

			statement1st.close();
			statement1st = connection1st.createStatement();
			String QueryString2nd = "SELECT url from qmsdb.qualityManual WHERE manual like 'StandardOperatingProcedures%'";
			ResultSet rs2nd = statement1st.executeQuery(QueryString2nd);
			while (rs2nd.next()) {
				manualList.add(rs2nd.getString(1));
			}
		    request.setAttribute("manualList", manualList);
			rs2nd.close();
			statement1st.close();
			connection1st.close();
			request.getRequestDispatcher("QM_SOP.jsp").forward(request, response);
		} catch (Exception ex) {
			System.out.println(ex);
		}		
	}
}
